﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using BusinessEntity;

namespace BusinessService
{
   public class UserService
    {
       public bool IsExitByUserId(string userId)
       {
           var sql = "select 1 from [user] where userid='" + userId+"'";
           var tb = SqlHelper.ExecuteDataTable(sql);
           return tb.Rows.Count > 0;
       }

       public int AddOneByUserId(string userId,string name)
       {
           var sql = "insert into [user] (userId,fullname,firstlogtime) values(@userId,@fullname,@firstlogtime)";
           return SqlHelper.ExecuteNonQuery(sql, 
               new SqlParameter("@userid",userId),
               new SqlParameter("@fullname",name),
               new SqlParameter("@firstlogtime",DateTime.Now)
               );
       }

       public int GetIdByUserId(string userId)
       {
           var sql = "select id from [user] where  userid='" + userId+"'";
           return (int)SqlHelper.ExecuteScalar(sql);
       }

       public int GetRoleIdByUserId(string userId)
       {
           var sql = "select roleid from [user] where userid='" + userId + "'";
           return (int)SqlHelper.ExecuteScalar(sql);
       }

       public User GetUserById(int id)
       {
           var sql = "select * from [user] where id=" + id;
           var dt = SqlHelper.ExecuteDataTable(sql);
           if (dt.Rows.Count < 0) return null;
           var row = dt.Rows[0] as DataRow;
           var user = new User()
           {
               Id=id,
               UserId = row["userId"].ToString(),
               FullName = row["fullname"].ToString(),
               RoleId = (int)row["roleid"],
           };
           return user;
       }

       public int GetFitestUserIdByArticleId(string workorder)
       {
           return (int)SqlHelper.ExecuteScalarSp("getFitestUserToRepair", new SqlParameter("@workorder", workorder));
       }

       public List<User> GetUsersByroleId(int roleId)
       {
           List<User> list = null;
           var sql = "select * from [User] where roleid=" + roleId;
           var result = SqlHelper.ExecuteDataTable(sql);
           if (result.Rows.Count != 0)
           {
               list = new List<User>();
               foreach (DataRow row in result.Rows)
               {
                   var user = new User()
                   {
                       Id = (int)row["id"],
                       UserId = row["userId"].ToString(),
                       FullName = row["fullname"].ToString(),
                       RoleId = roleId,
                   };
                   list.Add(user);
               }
           }
           return list;
       }

       public User GetUserByUserId(string userId)
       {
           String sql = "select * from [user] where userid='" + userId + "'";
           var result = SqlHelper.ExecuteDataTable(sql);
           if(result.Rows.Count>0)
           {
               var row = result.Rows[0];
               var user = new User()
               {
                    Id = (int)row["id"],
                       UserId = row["userId"].ToString(),
                       FullName = row["fullname"].ToString(),
                       RoleId = Convert.ToInt32(row["roleid"])
               };
               return user;
           }
           return null;
       }
      
    }
}
